package top.huxy.boot.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname PojoUtils
 * @Description PojoUtils
 * @Date 2021/9/5 8:57
 * @Created by huxy
 */
@Slf4j
public class PojoUtils {
    public static Map<String,Object> pojo2map(Object any){
        if(any==null) throw new NullPointerException();
        HashMap<String,Object> map=new HashMap<>();
        Field[] declaredFields = any.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String name = field.getName();
            try {
                Object o = field.get(any);
                if(o!=null) map.put(name,o);
            } catch (IllegalAccessException e) {
                log.error("不应该错误",e);
            }
        }
        return map;
    }
}
